package su.msk.jet.team01.chatik.client.ui.swing;

import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("serial")
public class MessageBox extends JPanel {
	private static final int DEFAULT_COLUMS_NUMBER = 80;
	private static final int DEFAULT_ROWS_NUMBER = 20;

	private final JTextArea textBox;

	private static Logger logger = LoggerFactory.getLogger(MessageBox.class);

	public MessageBox() {
		final int numb = 5;
		final JScrollPane scrollPane;
		textBox = new JTextArea(DEFAULT_ROWS_NUMBER, DEFAULT_COLUMS_NUMBER);
		textBox.setEditable(false);
		textBox.setLineWrap(true);
		textBox.setWrapStyleWord(true);
		this.add(textBox);

		scrollPane = new JScrollPane(textBox);
		this.add(scrollPane);

		setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
		setLayout(new GridLayout(0, 1, 0, numb));
	}

	private void scroll() {
		textBox.setCaretPosition(textBox.getDocument().getLength());
	}

	void println(final String str) {
		textBox.append(str);
		textBox.append("\n");

		scroll();

		logger.info(str);
	}
}